﻿Imports DataObject
Imports BusinessObject
Imports Microsoft.ApplicationBlocks.Data

Public Class frmNhanVienList

    Private oNhanVien As New BusNhanVien
    Private oPrNhanVien As New NhanVien
    Private oConn As New hDBase
    Private cboViTriCongViec As New Infragistics.Win.UltraWinGrid.UltraCombo
    Private ds As New DataSet
    Private dsQuanLyHopDong As New dsQLHD
    Private bCheck As Boolean = False

    Private Sub frmNhanVienList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitData()
        LoadGridView()
        RefreshToolbar()
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_ViTriCongViecs", dsQuanLyHopDong, New String() {"ViTriCongViec"})
        DataBindingToControl(UltraTabPageControl1)
        If (lbGioiTinh.Text.Equals("True") = True) Then
            lbGioiTinh.Text = "Nam"
        Else
            lbGioiTinh.Text = "Nữ"
        End If
        Me.tabMain.Tabs("tabThongTinChung").Text = "Thông tin chung"
    End Sub

    Protected Overrides Sub LoadDataGrid()
        Try
            tableDictionary = oNhanVien.GetAllNhanVien(sConn)
            ListBindingSource.DataSource = tableDictionary
            grdList.DataSource = ListBindingSource
            LoadAllCombo()
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub LoadGridView()
        Try
            grdList.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect
            For i As Integer = 0 To grdList.DisplayLayout.Bands(0).Columns.Count - 1
                grdList.DisplayLayout.Bands(0).Columns(i).Hidden = True
            Next

            grdList.DisplayLayout.Bands(0).Columns("MaNV").Hidden = False
            grdList.DisplayLayout.Bands(0).Columns("TenNV").Hidden = False
            grdList.DisplayLayout.Bands(0).Columns("NgaySinh").Hidden = False
            grdList.DisplayLayout.Bands(0).Columns("SoViTriCV").Hidden = False

            grdList.DisplayLayout.Bands(0).Columns("MaNV").Header.Caption = "Mã nhân viên"
            grdList.DisplayLayout.Bands(0).Columns("TenNV").Header.Caption = "Tên nhân viên"
            grdList.DisplayLayout.Bands(0).Columns("NgaySinh").Header.Caption = "Ngày sinh"
            grdList.DisplayLayout.Bands(0).Columns("SoViTriCV").Header.Caption = "Chức vụ"
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Protected Overrides Sub InitAddNewRow(ByVal oRow As System.Data.DataRow)
        With oRow
            .Item("MaNV") = ""
            .Item("TenNV") = ""
            .Item("GioiTinh") = True
        End With
        bCheck = True
    End Sub

    Protected Overrides Function DeleteDictionary() As Boolean
        oPrNhanVien.SoNV = iValue
        oNhanVien.DeleteNhanVien(sConn, oPrNhanVien)
    End Function

    Private Sub grdList_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdList.MouseClick
        Try
            iValue = grdList.ActiveRow.Cells(TablePrimaryKey).Value
        Catch ex As Exception
        End Try
    End Sub

    Private Sub LoadAllCombo()
        ds.Clear()
        If (Not ds.Tables("ViTriCongViec") Is Nothing) Then
            ds.Tables("ViTriCongViec").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_ViTriCongViecs", ds, New String() {"ViTriCongViec"})
        With cboViTriCongViec
            .DataSource = ds.Tables("ViTriCongViec")
            .DisplayMember = "TenViTriCV"
            .ValueMember = "SoViTriCV"
            .DisplayLayout.Load(PathLayout & "cboChucVu.lyt")
        End With
        grdList.DisplayLayout.Bands(0).Columns("SoViTriCV").EditorControl = cboViTriCongViec
        grdList.DisplayLayout.Bands(0).Columns("SoViTriCV").CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit
    End Sub

    Private Sub DataBindingToControl(ByVal oContainer As Control)
        Try
            Dim myControl As Control
            Dim strTag As String = Nothing
            If Not IsNothing(ListBindingSource) Then
                For Each myControl In oContainer.Controls
                    strTag = CStr(myControl.Tag)
                    If Not IsNothing(strTag) AndAlso strTag <> "" Then
                        If (myControl.GetType Is GetType(Infragistics.Win.Misc.UltraLabel)) Then
                            myControl.DataBindings.Add("Text", ListBindingSource, strTag)
                        End If
                    Else
                        If myControl.Controls.Count > 0 Then
                            DataBindingToControl(myControl)
                        End If
                    End If
                Next
            End If
            lbChucVu.Text = cboViTriCongViec.Text
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Protected Overrides Sub ChangeListBindingSourcePosition()
        Try
            If (Not ListBindingSource.IsBindingSuspended) AndAlso (ListBindingSource.Position <> -1) Then
                Dim oRow As DataRow
                oRow = GetCurrentRow(ListBindingSource)
                If (bCheck = False) Then
                    If (lbGioiTinh.Text.Equals("True") = True) Then
                        lbGioiTinh.Text = "Nam"
                    Else
                        lbGioiTinh.Text = "Nữ"
                    End If
                    lbChucVu.Text = dsQuanLyHopDong.ViTriCongViec.FindBySoViTriCV(lbChucVu.Text).TenViTriCV
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

End Class
